package com.example.vv.model.setupTreeItem;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.widget.TextView;

import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.adapter.RecyclerViewHolder;
import com.example.vv.fragmentWork.entity.TreeEntity;
import com.example.vv.fragmentWork.mvp.MvpFragment;
import com.example.vv.model.setupTreeItem.base.contract.STIContract;
import com.example.vv.model.setupTreeItem.base.presenter.STIPresenter;
import com.example.vv.model.setupTreeList.SetupTreeListActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Random;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class STIFragment extends MvpFragment<STIContract.View, STIPresenter> implements STIContract.View {

    String id;
    @BindView(R.id.title)
    TextView title;

    public STIFragment(String id) {
        this.id = id;
    }

    private Paint mSearchHistoryPaint;
    private int mPaddingSize;
    @BindView(R.id.flow_recycler)
    RecyclerView flowRecycler;

    @Override
    public int initLayout() {

        return R.layout.s_t_i_fragment;
    }

    @Override
    public STIPresenter initPresenter() {
        return new STIPresenter();
    }

    @Override
    public void initView() {

    }

    @Override
    public void lazyLoad() {

    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        title.setText(id);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleBar() {
        return null;
    }


    @Override
    public Context getMContext() {
        return getActivity();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getData(List<TreeEntity.ChildrenBean> childrenBeans) {
        BaseRecyclerViewAdapter<TreeEntity.ChildrenBean> adapter = new BaseRecyclerViewAdapter<TreeEntity.ChildrenBean>(getActivity(), childrenBeans, R.layout.item_flow_tv) {
            @Override
            protected void onBindData(RecyclerViewHolder holder, TreeEntity.ChildrenBean bean, int position) {
                Random random = new Random();
                int y = random.nextInt(255);
                int z = random.nextInt(255);
                int h = random.nextInt(255);
                TextView textView = (TextView) holder.getView(R.id.tv);
                textView.setText(bean.getName());
                textView.setTextColor(Color.argb(255, y, z, h));
                textView.setOnClickListener(v->{
                    Intent intent = new Intent(getActivity(), SetupTreeListActivity.class);
                    intent.putExtra("id",bean.getId());
                    intent.putExtra("name",bean.getName());
                    startActivity(intent);
                });
            }
        };
        flowRecycler.setHasFixedSize(true);
        mSearchHistoryPaint = new Paint();//画笔用来计算文字的宽度
        //设置文字的大小，注意布局文件里写的是sp，这里需要进行转换成对应的px，我这里就不写了
        mSearchHistoryPaint.setTextSize(70);
        //textview的大小，包括文字大小和左右内边距，所以这个值表示内边距大小，同样注意转换
        mPaddingSize = 100;
        //获取屏幕的宽度，我这里rccyclerview正好是屏幕的宽度，否则应替换为rec的宽度
        final int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        //这里讲网格的数量设置为屏幕的宽度，网格无限小
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), width);
        //设置每个文字占据的网格数
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //就是这里 需要测量文字的宽度，加上左右内边距
                int textWidth = (int) mSearchHistoryPaint.measureText(childrenBeans.get(position).getName()) + mPaddingSize;
                //如果文字的宽度超过屏幕的宽度，那么我们就设置为屏幕宽度
                return textWidth > width ? width : textWidth;
            }
        });
        flowRecycler.setLayoutManager(gridLayoutManager);
        flowRecycler.setAdapter(adapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}